/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlparser;

import DataBase.Coleccao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import DataBase.Ficheiro;

public class LinkChanger {

    //private Coleccao colection = new Coleccao();
    Coleccao listagemBD = Coleccao.getInstance();

    public LinkChanger() {
    }

    public Boolean linkChanger2local(Ficheiro obj) throws IOException {
        BufferedReader reader = null;
        String path = obj.getCaminholocal();

        try {
            reader = new BufferedReader(new FileReader(path));
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }

        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");

        try {
            while ((line = reader.readLine()) != null) {
                if ((line.contains("href") || line.contains("HREF") || line.contains("SRC"))
                        || (line.contains("src") || line.contains("HRef") || line.contains("Src"))
                        || (line.contains("value") || line.contains("Value") || line.contains("VALUE"))) {
                    line = changeLink(line, obj);
                    stringBuilder.append(line);
                    stringBuilder.append(ls);
                } else {
                    stringBuilder.append(line);
                    stringBuilder.append(ls);
                }
            }

            boolean fileResult = false;

            fileResult = fileWriter(obj.getCaminholocal(), stringBuilder.toString());
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public String changeLink(String ressource, Ficheiro obj) {
        String regex = getRegex(ressource, obj);
        if (!regex.equals("")) {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(ressource);
            StringBuffer sb = new StringBuffer();
            while (m.find()) {
                // For example: transform match to upper case
                String replacement = "./";
                m.appendReplacement(sb, replacement);
            }
            m.appendTail(sb);
            ressource = sb.toString();
        }
        return ressource;
    }

    public String getRegex(String ressource, Ficheiro obj) {
        //Estas variaveis terão que ser preenchidas com o dominio do site dinamicamentente
        String cursor[] = {"c:/"};
        StringBuilder regex = new StringBuilder();

        //It gets all cursors to look for i.e. c:/, ./
        for (int i = 0; i < cursor.length; i++) {
            if (ressource.contains(cursor[i])) {
                regex.append(cursor[i]);
            }
        }
        if (ressource.contains(obj.getDominio())) {
            regex.append(obj.getDominio());
        }


        Date updated = new Date();
        String st;
        if (regex.toString().toLowerCase().contains("htm")) {
            st = obj.DB_STATE_DOWNLOAD;
        } else {
            st = obj.DB_STATE_COMPLETE;

        }

        return regex.toString();
    }

    public boolean fileWriter(String file, String value) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(value);

        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                return false;
            }
        }
        return true;
    }
}
